package com.itheima.exerciseTest.method30_39.method39;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

//        2.5.1Class类获取成员方法对象的方法
public class Demo {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
//        Method[] getMethods()                   	返回所有公共成员方法对象的数组，包括继承的
        // method1();

//        Method[] getDeclaredMethods()           	返回所有成员方法对象的数组，不包括继承的
        // method2();

//        Method getMethod(String name, Class<?>... parameterTypes)	返回单个公共成员方法对象
        // method3();

//        Method getDeclaredMethod(String name, Class<?>... parameterTypes)	返回单个成员方法对象
        // method4();


        // method5();

    }

    private static void method5() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class className = Class.forName("com.itheima.exerciseTest.method30_39.method39.Student");
        Method method = className.getMethod("function4", String.class);
        Student student = (Student) className.newInstance();
        String name = (String) method.invoke(student, "刘德华");
        System.out.println(name);
    }

    private static void method4() throws ClassNotFoundException, NoSuchMethodException {
        Class className = Class.forName("com.itheima.exerciseTest.method30_39.method39.Student");
        Method method = className.getDeclaredMethod("show");
        System.out.println(method);
    }

    private static void method3() throws ClassNotFoundException, NoSuchMethodException {
        Class className = Class.forName("com.itheima.exerciseTest.method30_39.method39.Student");
        Method method = className.getMethod("function1");
        System.out.println(method);
    }

    private static void method2() throws ClassNotFoundException {
        Class className = Class.forName("com.itheima.exerciseTest.method30_39.method39.Student");
        Method[] declaredMethods = className.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }
    }

    private static void method1() throws ClassNotFoundException {
        Class className = Class.forName("com.itheima.exerciseTest.method30_39.method39.Student");
        Method[] methods = className.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
    }
}
